<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" type="text/css" href="../plugins/layuiadmin/layui/css/layui.css" media="all">
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<%
String url = "";
if(request.getParameter("coClass")!=null) {
	url += "coClass="+request.getParameter("coClass");
}
%>
<body>
	<jsp:include page="systemrolevalidate.jsp" flush="true">
		<jsp:param name="m" value="systempower"/>
	</jsp:include>
	<blockquote class="layui-elem-quote news_search">
		<form action="" class="layui-form" id="searchform">
			<div class="layui-inline">
			    <div class="layui-input-inline">
			    	<input value="" id="coName" name="coName" placeholder="名称" class="layui-input search_input" type="text">
			    </div>
			</div>
			<div class="layui-inline">
			    <div class="layui-input-inline">
			    	<input value="" id="coValue" name="coValue" placeholder="内容" class="layui-input search_input" type="text">
			    </div>
			</div>
			<div class="layui-inline">
			    <a class="layui-btn" lay-submit lay-filter="btnSearch">查询</a>
			</div>
		</form>
	</blockquote>
 
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
	<button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delDatas">批量删除</button>
  </div>
</script>
 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/javascript" src="../plugins/layuiadmin/layui/layui.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 --> 
 
<script>
var $;
layui.use(['jquery'], function(){ 
   $ = layui.$;
});
var table;
layui.use(['table','form'], function(){
  table = layui.table
  ,form = layui.form;
  
  table.render({
    elem: '#test'
    ,url:'../anConfigList.action?<%=url%>'
    ,where: {time:new Date().getTime()}
    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
    ,toolbar: '#toolbarDemo'
    ,title: '数据'
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field:'coId', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
      ,{field:'coClass', title:'类别', width:120, sort: true}
      ,{field:'coName', title:'名称', width:200, edit: 'text', sort: true}
      ,{field:'coValue', title:'内容', edit: 'text', sort: true}
      ,{field:'coType', title:'属性', width:120}
      ,{field:'coOrder', title:'排序', width:120, edit: 'text',sort: true} 
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
    ]]
    ,response: {
      statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
    }
    ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
    	//console.log(JSON.stringify(res.rows))
    	this.limits=getLimits(res.total);
      return {
        "code": 200, //解析接口状态
        "msg": res.message, //解析提示文本
        "count": res.total, //解析数据长度
        "data": res.rows //解析数据列表
      };
    }
    ,page: true
    ,id:"table1"
  });
  //单元格编辑
  //监听单元格编辑
  table.on('edit(test)', function(obj){
    var value = obj.value //得到修改后的值
    ,data = obj.data //得到所在行所有键值
    ,field = obj.field; //得到字段
    console.info(data);
    $.ajax({
		type:"post",
		url:"../anConfigUpdate.action",
		data:{
			rowstr:JSON.stringify(data)
		},
		async:true,
		cache:false,
		success:function(res){
			layer.msg('提交成功');
		}
	 });
    layer.msg('[ID: '+ data.coId +'] ' + field + ' 字段更改为：'+ value);
  });
  //头工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    //console.log(checkStatus.data)
    switch(obj.event){
      case 'delDatas':
        layer.confirm('确定删除选中行？', function(index){
					layer.close(index);
		      $.ajax({
		      	type:"post",
		      	url:"../AnAcquisitionHistoryDeletes.action",
		      	data:{
		      		rowstr:JSON.stringify(checkStatus.data)
		      	},
		      	cache:false,
		      	async:true,
		      	success:function(res){
		      		if(res.flag === 'success'){
		      			layer.msg('删除成功');
		      			table.reload('table1', {where: {time:new Date().getTime()}});
		      		}else{
		      			layer.msg('删除失败')
		      		}
		      	}
		      });
		      //向服务端发送删除指令
		    });
      break;
      case 'add':
		layer.open({
			type: 2,
			title: "新增配置",
			area: ['40%', '60%'],
			fix: false,
			maxmin: true,
			shadeClose: true,
			shade: 0.4,
			skin: 'layui-layer-rim',
			content:["anConfigAdd.jsp?<%=url%>", "yes"],
		});
	  break;
    };
  });
  //监听工具条
  table.on('tool(test)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
    var data = obj.data; //获得当前行数据
    var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
    var tr = obj.tr; //获得当前行 tr 的DOM对象
    if(layEvent === 'detail'){ //查看
      //do somehing
    } else if(layEvent === 'del'){ //删除
      	layer.confirm('真的删除行么', function(index){
        layer.close(index);
        $.ajax({
        	type:"post",
        	url:"../anConfigDelete.action",
        	data:{
        		rowstr:JSON.stringify(obj.data)
        	},
        	async:true,
        	cache:false,
        	success:function(res){
        		//console.log(res)
        		if(res.flag === 'success'){
        			layer.msg('删除成功')
        			table.reload('table1', {where: {time:new Date().getTime()}});
        		}else{
        			layer.msg('删除失败')
        		}
        	}
        });
        //向服务端发送删除指令
      });
    }  else if(layEvent === 'edit'){ //编辑
    	layer.open({
			type: 2,
			title: "修改",
			area: ['80%', '100%'],
			fix: false,
			maxmin: true,
			shadeClose: true,
			shade: 0.4,
			skin: 'layui-layer-rim',
			content:["anConfigEdit.jsp?coId="+data.coId, "yes"],
		});
  	}
  });
  //监听提交
  form.on('submit(btnSearch)', function(data){
    var rowData = data.field;
    console.info(rowData);
		table.reload('table1', {
		  url:'../anConfigList.action?<%=url %>'
		  ,where: {
		  	"coName":rowData.coName,
		  	"coValue":rowData.coValue
		  } //设定异步数据接口的额外参数
		  //,height: 300
		});
    return false;
  });
});

function getLimits(total){
	var arr=[10],i=0;
	addLimit();
	function addLimit(){
		if(total>arr[i]){
			var d = arr[i]*2;
			if(total>d && d<100){
				arr.push(d);
				i++;
				addLimit();
			}else{
				arr.push(total);
			}
		}
	}
	return arr;
}
</script>

</body>
</html>